package ifi.p17.jarmanagement.model;

import ifi.p17.jarmanagement.MainActivity;
import ifi.p17.jarmanagement.R;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnLongClickListener;
import android.widget.EditText;
import android.widget.TextView;

public class Revenue {

	final public static int AMOUNT_DEFAULT = 0;
	final public static String UNIT_DEFAULT = "$";

	MainActivity atv;
	int amount, remaining;
	String unit;
	TextView tv_revenu, tv_remaining;
	
	public Revenue(MainActivity atv) {
		this.atv = atv;
		this.amount = AMOUNT_DEFAULT;
		this.unit = UNIT_DEFAULT;
		this.remaining = this.amount;
	}

	public void updateText(){
		tv_revenu.setText("Revenu: " + Integer.toString(amount) + "(" + unit + ")");
		tv_remaining.setText("Remaining: " + Integer.toString(remaining) + "(" + unit + ")");
	}
	
	public TextView getTv_revenu() {
		return tv_revenu;
	}

	public void setTv_revenu(TextView tv_jar) {
		this.tv_revenu = tv_jar;
		controlTVRevenu();
	}

	public TextView getTv_remaining() {
		return tv_remaining;
	}

	public void setTv_remaining(TextView tv_remaining) {
		this.tv_remaining = tv_remaining;
	}

	public int getAmount() {
		return amount;
	}

	public void setAmount(int amount) {
		this.amount = amount;
	}

	public String getUnit() {
		return unit;
	}

	public void setUnit(String unit) {
		this.unit = unit;
	}

	public int getRemaining() {
		return remaining;
	}

	public void setRemaining(int remaining) {
		this.remaining = remaining;
	}

	@Override
	public String toString() {
		return unit + Integer.toString(amount);
	}
	
	private void controlTVRevenu() {
		tv_revenu.setOnLongClickListener(new OnLongClickListener() {
			
			@Override
			public boolean onLongClick(View v) {
				longClickActionTVRevenu(v);
				return false;
			}
		});
	}

	protected void longClickActionTVRevenu(View v) {
		
		LayoutInflater layoutInflater = LayoutInflater.from(v.getContext());
		
		View promptView = layoutInflater.inflate(R.layout.jar_input, null);

		AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(v.getContext());

		// set prompts.xml to be the layout file of the alertdialog builder
		alertDialogBuilder.setView(promptView);

		
		TextView tv_input1 = (TextView) promptView.findViewById(R.id.input_tv1);
		TextView tv_input2 = (TextView) promptView.findViewById(R.id.input_tv2);
		tv_input1.setText("Revenu: ");
		tv_input2.setText("Unit: ");
		
		final TextView cur_time = (TextView) promptView.findViewById(R.id.jar_input_cur_time);
		final EditText input = (EditText) promptView.findViewById(R.id.jar_input_purchase);
		final EditText description = (EditText) promptView.findViewById(R.id.jar_input_purchase_des);

		cur_time.setText("Set your revenu:");
		input.setText(Integer.toString(amount));
		description.setText(unit);
		
		// setup a dialog window

		alertDialogBuilder
				.setCancelable(false)
				.setPositiveButton("OK", new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int id) {
								// get user input and set it to result
								if (input.getText().toString().length() != 0){
									int pur = Integer.parseInt(input.getText().toString());
									String u = description.getText().toString();
									updateNewRevenu(pur, u);
								}
							}
						})
				.setNegativeButton("Cancel",
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog,	int id) {
								dialog.cancel();
							}
						});

		// create an alert dialog
		AlertDialog alertD = alertDialogBuilder.create();

		alertD.show();
		
	}

	protected void updateNewRevenu(int pur, String u) {
		if (pur < amount-remaining || u.length() == 0){
			String err = "";
			if (pur<remaining) err = "Revenu must be bigger than remaining !";
			else if (u.length() == 0) err = "Unit must not empty!";
			
			// out of total
			AlertDialog.Builder builder = new AlertDialog.Builder(tv_revenu.getContext());
			
			builder.setMessage(err)
					.setTitle("Warning");
			
			builder
				.setCancelable(false)
				.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int which) {
						dialog.cancel();
					}
				});
			
			AlertDialog alert = builder.create();
			alert.show();
			
		} else {
			amount = pur;
			unit = u;
			atv.calculateTotalEachJar();
			atv.drawWindow();
		}
	}


	
}
